package fr.zadoka.zadokadna.Loader;

import java.util.GregorianCalendar;

import Cache.ImageCache;
import android.R.integer;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;

public class UrlImageLoaderTask extends AsyncTask<GregorianCalendar, integer, Bitmap>
{
	ImageCache mImageCache;
	TaskCallback<Bitmap> mCallback;
	
	
	public UrlImageLoaderTask(ImageCache imageCache, TaskCallback<Bitmap> callback)
	{
		mImageCache = imageCache;
		mCallback = callback;
	}

	@Override
	protected Bitmap doInBackground(GregorianCalendar... arg0) {
	        
	    GregorianCalendar imageDate = arg0[0];
	        
	    Bitmap zadokaImage = mImageCache.getBitmapFromMemCache(imageDate);
	    
	    if (zadokaImage != null)
	    	return zadokaImage;
	    
		Log.i("MyFirstApp", "ImageLoaderAsyncTask.doInBackground downloading from web");
		
		zadokaImage = new ZadokaImageLoader().getImage(imageDate);
	    			    
		if (zadokaImage != null)
			mImageCache.addBitmapToMemoryCache(imageDate, zadokaImage);
		
	    return zadokaImage;		
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		mCallback.OnResult(result);
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(integer... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
	}
	
}
